package hzmg.akkacloud.server.web.response;

import hzmg.akkacloud.server.persistence.remote.model.AppInfoDO;
import hzmg.akkacloud.server.remote.worker.WorkerClusterQueryService;
import lombok.Data;
import org.springframework.beans.BeanUtils;

/**
 * app信息对外展示
 *
 * @author zbw
 * @since 2022/7/26
 */
@Data
public class AppInfoVO {
    private Long appId;
    private String appName;
    private Integer appNumber;
    private Integer appHealthyNum;


    public static AppInfoVO from(AppInfoDO afDO, WorkerClusterQueryService workerClusterQueryService) {
        AppInfoVO vo = new AppInfoVO();
        BeanUtils.copyProperties(afDO, vo);
        Long id = afDO.getId();
        int workSize = workerClusterQueryService.getAllWorkers(id).size();
        if (workSize == 0) {
            return null;
        }

        vo.setAppId(afDO.getId());
        vo.setAppNumber(workerClusterQueryService.getAllWorkers(id).size());
        vo.setAppHealthyNum(workerClusterQueryService.getAllAliveWorkers(id).size());
        return vo;
    }
}
